*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 


*------------------------------------------------------------------------------*
*	Step 11- Testing SCM Case Study for One City 	   *
*------------------------------------------------------------------------------*

	set more off 
	use "$build/01_annualizeddatawcontrol.dta", replace
	
	*Keep only buildings in DC 
	keep if city == "Washington" & state == "DC" // drop 21974 observations, 845 obs for 151 buildings 
	
	*Now just pick one treated building to test running SCM 
	
	sort propnum year
	keep if treat == 0 | address1=="355 & 375 E Street SW" & city == "Washington" // one treated building 
	*Make the dataset a balanced panel 
	drop if year<=2010 
	drop if year>=2015
	bysort propnum: gen nyear=[_N]
	keep if nyear==4 
	
	
	
	loc matching_vars "Covered_E real_elecprice real_gasprice Unemployment HDD CDD age2 age used_space percentleased real_capex_ti real_capex_bldimp " // variable used in matching
	loc xvar "propnum"              // buildings or msas or whatever
	loc tvar "year"              // time variable (year?)
	loc treatperiod "2009"       // 1st period where treatment occurs
	loc outcome "logrealrentpersf"           // outcome variable
	drop if missing(age)
	
	
	//----------- Read in data and get a list of all ----------//
	xtset `xvar' `tvar'
	levelsof `xvar', clean local(xvals)		// this local macro will have each unit to loop through (treated and placebos)

	
	//------ Run synth for treated unit and each potential placebo
	foreach l in `xvals' {
		di `l'
		preserve
			tempfile t`l'
			#delimit ;
			synth `outcome' 
				  `matching_vars'
				, trperiod(`treatperiod') trunit(`l') keep(`t`l'')					// this will save a dataset with results for each run of synth command (tempfile called `t`l'')
			;
			#delimit cr
exit 5
		restore
		}	
	
	//------- If you want the donor weights for your treated unit
	foreach l in `xvals' {
	use "`t`l''", clear					// use the results for the treated unit here (where `l' is the value for the actual treated unit)					
	rename (_Co_Number _W_Weight) (matched_unit weight)
	keep matched_unit weight
	drop if mi(matched_unit)
	save "$analysis/01_test_synth_weights`l'", replace
	}

	
	//------- Append all the results from each run of synth and get your distribution of effects
	clear
	loc flag 0

	foreach l in `xvals' {
		append using "`t`l''"
		if `flag' == 0 {
			gen `xvar' = `l'
			loc ++flag
		}
		keep _Y_treated _Y_synthetic _time `xvar'
		replace `xvar' = `l' if mi(`xvar')
	}

	// calculate differences
	rename (_Y_treated _Y_synthetic _time) (_y_treat _y_synth `tvar')
	gen diff = _y_treat - _y_synth
	drop if mi(`tvar')
	format %ty year
	save "$analysis/02_test_synth_placebo_dataset", replace				// this dataset has 5 variables:
																			// (1) `xvar' is the unit that synth was run for
																			// (2) `tvar' is the time variable
																			// (3) _y_treat is the actual outcome variable
																			// (4) _y_synth is the synthetic outcome variable
																			// (5) difference is the treatment effect for that unit
																			


